<?php
/**
 * Created By base
 * Author: Virus
 * Date: 2020/4/4
 * Time: 17:38
 */

namespace app\common\logic;


use app\common\model\mysql\Article;

class ArticleLogic
{
    protected $model;

    public function __construct()
    {
        $this->model = new Article();
    }

    public function getList()
    {
        $result = $this->model->with(['categories'])->select();
        if (empty($result)) {
            return [];
        }

        return $result->toArray();
    }

    /**
     * @param array $data
     * @return bool
     * @throws \think\Exception
     */
    public function addArticle(array $data)
    {
        $data['admin_id'] = session('uid');
        $result           = $this->model->addData($data);
        if (!$result) {
            throw new \think\Exception("添加失败");
        }

        return true;
    }

    /**
     * @param $id
     * @return array|mixed
     */
    public function getDataOneById($id)
    {
        $result = $this->model->getOneDataById($id);
        if (empty($result)) {
            return [];
        }

        return $result;
    }

    /**
     * 编辑文章
     * @param $id
     * @param $data
     * @return bool
     * @throws \think\Exception
     */
    public function updateCategory($id, $data)
    {
        $result = $this->model->updateDataById($id, $data);
        if (false === $result) {
            throw new \think\Exception("文章编辑失败");
        }

        return true;
    }

    /**
     * 删除文章
     * @param $id
     * @return mixed
     * @throws \think\Exception
     */
    public function deleteById($id)
    {
        $article = Article::find($id);
        $result  = $article->delete();

        return $result;
    }

    public function restoreData($id)
    {
        $article = Article::onlyTrashed()->find($id);
        $result  = $article->restore();

        return $result;
    }

    public function destroyArticle($id)
    {
        return Article::destroy($id, true);
    }
}